<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="车牌号" prop="licensePlate">
              <el-input v-model="queryParams.licensePlate" placeholder="请输入车牌号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="车型" prop="model">
              <el-select v-model="queryParams.model" placeholder="请选择车型">
                <el-option v-for="dict in clxh" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="使用性质" prop="usageNature">
              <el-select v-model="queryParams.usageNature" placeholder="请选择使用性质">
                <el-option v-for="dict in syxz" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['vehicle:vehicleBasicInfo:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['vehicle:vehicleBasicInfo:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['vehicle:vehicleBasicInfo:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['vehicle:vehicleBasicInfo:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="vehicleBasicInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="车辆图片" align="center" prop="vehiclePicUrl">
          <template #default="scope">
            <image-preview :src="scope.row.vehiclePicUrl" height="50" />
          </template>
        </el-table-column>
        <el-table-column label="车牌号" align="center" prop="licensePlate">
          <template #default="scope">
            <el-link @click="handleInfo(scope.row)" type="primary">{{ scope.row.licensePlate }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="车型" align="center" prop="model">
          <template #default="scope">
            <dict-tag :options="clxh" :value="scope.row.model || ''" />
          </template>
        </el-table-column>
        <el-table-column label="使用性质" align="center" prop="usageNature">
          <template #default="scope">
            <dict-tag :options="syxz" :value="scope.row.usageNature ? scope.row.usageNature.split(',') : []" />
          </template>
        </el-table-column>
        <el-table-column label="购置日期" align="center" prop="purchaseDate" width="180">
          <template #default="scope">
            <el-tooltip :content="scope.row.purchaseDateStatus === 0 ? '正常' : scope.row.purchaseDateStatus === 1 ? '临期' : '超期'">
              <el-icon
                :style="{
                  color: scope.row.purchaseDateStatus === 0 ? '#67C23A' : scope.row.purchaseDateStatus === 1 ? '#E6A23C' : '#F56C6C',
                  fontSize: '16px',
                  verticalAlign: 'middle'
                }"
              >
                <i class="el-icon-circle" />
              </el-icon>
            </el-tooltip>
            <span style="margin-left: 6px">{{ parseTime(scope.row.purchaseDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="保险期限" align="center" prop="insuranceEndDate" width="180">
          <template #default="scope">
            <el-tooltip :content="scope.row.insuranceStatus === 0 ? '正常' : scope.row.insuranceStatus === 1 ? '临期' : '超期'">
              <el-icon
                :style="{
                  color: scope.row.insuranceStatus === 0 ? '#67C23A' : scope.row.insuranceStatus === 1 ? '#E6A23C' : '#F56C6C',
                  fontSize: '16px',
                  verticalAlign: 'middle'
                }"
              >
                <i class="el-icon-circle" />
              </el-icon>
            </el-tooltip>
            <span style="margin-left: 6px">{{ parseTime(scope.row.insuranceEndDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="年检有效期" align="center" prop="validityEndDate" width="180">
          <template #default="scope">
            <el-tooltip :content="scope.row.validityStatus === 0 ? '正常' : scope.row.validityStatus === 1 ? '临期' : '超期'">
              <el-icon
                :style="{
                  color: scope.row.validityStatus === 0 ? '#67C23A' : scope.row.validityStatus === 1 ? '#E6A23C' : '#F56C6C',
                  fontSize: '16px',
                  verticalAlign: 'middle'
                }"
              >
                <i class="el-icon-circle" />
              </el-icon>
            </el-tooltip>
            <span style="margin-left: 6px">{{ parseTime(scope.row.validityEndDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['vehicle:vehicleBasicInfo:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['vehicle:vehicleBasicInfo:remove']"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改车辆基本信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="60%" append-to-body>
      <el-form ref="vehicleBasicInfoFormRef" :model="form" :rules="rules" label-width="80px">
        <div class="title-name mb20">车辆基本信息</div>
        <el-form-item label="车辆图片" prop="vehiclePic">
          <image-upload v-model="form.vehiclePic" />
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="车牌号" prop="licensePlate">
              <el-input maxlength="10" v-model="form.licensePlate" placeholder="请输入车牌号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车型" prop="model">
              <el-select v-model="form.model" placeholder="请选择车型">
                <el-option v-for="dict in clxh" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="车辆品牌" prop="brand">
              <el-input maxlength="50" v-model="form.brand" placeholder="请输入车辆品牌" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车身颜色" prop="color">
              <el-input maxlength="10" v-model="form.color" placeholder="请输入车身颜色" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="购置日期" prop="purchaseDate">
              <div style="width: 100%">
                <el-date-picker
                  style="width: 100%"
                  clearable
                  v-model="form.purchaseDate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择购置日期"
                >
                </el-date-picker>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用性质" prop="usageNature">
              <el-select multiple v-model="form.usageNature" placeholder="使用性质">
                <el-option v-for="dict in syxz" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="座位数" prop="seatNum">
              <el-input v-model="form.seatNum" placeholder="请输入座位数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排量" prop="displacement">
              <el-select v-model="form.displacement" placeholder="请选择排量">
                <el-option v-for="dict in pl" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注信息" prop="remarks">
          <el-input :rows="4" maxlength="200" show-word-limit v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <div class="title-name mb20">车辆保险信息</div>
        <div>
          <div class="mb10"><el-button type="primary" icon="Plus" @click="addDomain">新增</el-button></div>
          <table width="100%" class="tableData" cellpadding="0" cellspacing="0" border="0">
            <tbody>
              <tr>
                <th width="5%">序号</th>
                <th width="14%">投保日期</th>
                <th width="17%">保险公司</th>
                <th width="17%">险种</th>
                <th width="20%">保险期限</th>
                <th width="17%">保费(元)</th>
                <th width="10%">操作</th>
              </tr>
            </tbody>
          </table>
          <table class="tableData" width="100%" cellpadding="0" cellspacing="0" border="1">
            <tbody>
              <tr v-for="(domain, index) in form.insuranceInfo" :key="index">
                <td width="5%">{{ index + 1 }}</td>
                <td width="14%">
                  <div style="width: 100%">
                    <el-date-picker
                      style="width: 100%"
                      clearable
                      v-model="domain.insuredDate"
                      type="date"
                      value-format="YYYY-MM-DD"
                      placeholder="请选择投保日期"
                    >
                    </el-date-picker>
                  </div>
                </td>
                <td width="17%">
                  <el-input maxlength="50" v-model="domain.insuranceCompany" placeholder="请输入保险公司" />
                </td>
                <td width="17%">
                  <el-input maxlength="50" v-model="domain.insuranceType" placeholder="请输入险种" />
                </td>
                <td width="20%">
                  <div style="width: 100%">
                    <el-date-picker
                      style="width: 100%"
                      @change="changeInsuranceDate($event, index)"
                      v-model="domain.insuranceDate"
                      type="daterange"
                      value-format="YYYY-MM-DD"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                    ></el-date-picker>
                  </div>
                </td>
                <td width="17%">
                  <el-input-number
                    style="width: 100%"
                    v-model="domain.premium"
                    :precision="2"
                    :min="0"
                    :max="999999999.99"
                    placeholder="请输入保费"
                  />
                </td>
                <td width="10%">
                  <el-button type="text" icon="Delete" @click="removeDomain(domain, 'insuranceInfo')">删除</el-button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="title-name mb20 mt20">车辆维修记录</div>
        <div>
          <div class="mb10"><el-button type="primary" icon="Plus" @click="addDomain1">新增</el-button></div>
          <table width="100%" class="tableData" cellpadding="0" cellspacing="0" border="0">
            <tbody>
              <tr>
                <th width="5%">序号</th>
                <th width="17%">维修日期</th>
                <th width="25%">厂家</th>
                <th width="25%">项目</th>
                <th width="18%">费用(元)</th>
                <th width="10%">操作</th>
              </tr>
            </tbody>
          </table>
          <table class="tableData" width="100%" cellpadding="0" cellspacing="0" border="1">
            <tbody>
              <tr v-for="(domain, index) in form.repairRecord" :key="index">
                <td width="5%">{{ index + 1 }}</td>
                <td width="17%">
                  <div style="width: 100%">
                    <el-date-picker
                      style="width: 100%"
                      clearable
                      v-model="domain.repairDate"
                      type="date"
                      value-format="YYYY-MM-DD"
                      placeholder="请选择维修日期"
                    >
                    </el-date-picker>
                  </div>
                </td>
                <td width="25%">
                  <el-input maxlength="50" v-model="domain.manufacturer" placeholder="请输入厂家" />
                </td>
                <td width="25%">
                  <el-input maxlength="50" v-model="domain.repairItem" placeholder="请输入项目" />
                </td>
                <td width="18%">
                  <el-input-number
                    style="width: 100%"
                    v-model="domain.cost"
                    :precision="2"
                    :min="0"
                    :max="999999999.99"
                    placeholder="请输入费用"
                    @update:model-value="(val) => (domain.cost = Number(val))"
                  />
                </td>
                <td width="10%">
                  <el-button type="link" icon="Delete" @click="removeDomain(domain, 'repairRecord')">删除</el-button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="title-name mb20 mt20">车辆年检记录</div>
        <div>
          <div class="mb10"><el-button type="primary" icon="Plus" @click="addDomain2">新增</el-button></div>
          <table width="100%" class="tableData" cellpadding="0" cellspacing="0" border="0">
            <tbody>
              <tr>
                <th width="5%">序号</th>
                <th width="25%">年检日期</th>
                <th width="35%">结果</th>
                <th width="25%">有效期</th>
                <th width="10%">操作</th>
              </tr>
            </tbody>
          </table>
          <table class="tableData" width="100%" cellpadding="0" cellspacing="0" border="1">
            <tbody>
              <tr v-for="(domain, index) in form.annualInspectionRecord" :key="index">
                <td width="5%">{{ index + 1 }}</td>
                <td width="25%">
                  <div style="width: 100%">
                    <el-date-picker
                      style="width: 100%"
                      clearable
                      v-model="domain.inspectionDate"
                      type="date"
                      value-format="YYYY-MM-DD"
                      placeholder="请选择年检日期"
                    >
                    </el-date-picker>
                  </div>
                </td>
                <td width="35%">
                  <el-input maxlength="50" v-model="domain.inspectionResult" placeholder="请输入结果" />
                </td>
                <td width="25%">
                  <div style="width: 100%">
                    <el-date-picker
                      style="width: 100%"
                      @change="changeValidityDate($event, index)"
                      v-model="domain.validityDate"
                      type="daterange"
                      value-format="YYYY-MM-DD"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                    ></el-date-picker>
                  </div>
                </td>
                <td width="10%">
                  <el-button type="link" icon="Delete" @click="removeDomain(domain, 'annualInspectionRecord')">删除</el-button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <detailDrawer ref="drawerRef"></detailDrawer>
  </div>
</template>

<script setup name="VehicleBasicInfo" lang="ts">
import {
  listVehicleBasicInfo,
  getVehicleBasicInfo,
  delVehicleBasicInfo,
  addVehicleBasicInfo,
  updateVehicleBasicInfo
} from '@/api/vehicle/vehicleBasicInfo/index';
import detailDrawer from './detailDrawer.vue';
import { VehicleBasicInfoVO, VehicleBasicInfoQuery, VehicleBasicInfoForm } from '@/api/vehicle/vehicleBasicInfo/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { syxz, clxh, pl } = toRefs<any>(proxy?.useDict('syxz', 'clxh', 'pl'));
const drawerRef = ref<any>();
const vehicleBasicInfoList = ref<VehicleBasicInfoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const dateRangePurchaseDate = ref<[DateModelType, DateModelType]>(['', '']);

const queryFormRef = ref<ElFormInstance>();
const vehicleBasicInfoFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: VehicleBasicInfoForm = {
  id: undefined,
  licensePlate: undefined,
  vehiclePic: undefined,
  brand: undefined,
  purchaseDate: undefined,
  seatNum: undefined,
  model: undefined,
  color: undefined,
  usageNature: undefined,
  insuranceInfo: [],
  repairRecord: [],
  annualInspectionRecord: [],
  displacement: undefined,
  remarks: undefined
};

const data = reactive<PageData<VehicleBasicInfoForm, VehicleBasicInfoQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    licensePlate: undefined,
    model: undefined,
    usageNature: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键ID不能为空', trigger: 'blur' }],
    licensePlate: [{ required: true, message: '车牌号不能为空', trigger: 'blur' }],
    vehiclePic: [{ required: true, message: '车辆图片不能为空', trigger: 'blur' }],
    brand: [{ required: true, message: '车辆品牌不能为空', trigger: 'blur' }],
    purchaseDate: [{ required: true, message: '购置日期不能为空', trigger: 'blur' }],
    seatNum: [
      {
        pattern: /^(0|\+?[1-9][0-9]*)$/,
        required: true,
        message: '请输入数量，且为正整数类型',
        trigger: 'blur'
      }
    ],
    model: [{ required: true, message: '车型不能为空', trigger: 'change' }],
    color: [{ required: true, message: '车身颜色不能为空', trigger: 'blur' }],
    usageNature: [{ required: true, message: '使用性质不能为空', trigger: 'blur' }],
    displacement: [{ required: true, message: '排量不能为空', trigger: 'change' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询车辆基本信息列表 */
const getList = async () => {
  loading.value = true;
  queryParams.value.params = {};
  proxy?.addDateRange(queryParams.value, dateRangePurchaseDate.value, 'PurchaseDate');
  const res = await listVehicleBasicInfo(queryParams.value);
  vehicleBasicInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  form.value.annualInspectionRecord = [];
  form.value.insuranceInfo = [];
  form.value.repairRecord = [];
  vehicleBasicInfoFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  dateRangePurchaseDate.value = ['', ''];
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: VehicleBasicInfoVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加车辆基本信息';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: VehicleBasicInfoVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getVehicleBasicInfo(_id);
  Object.assign(form.value, res.data);
  form.value.usageNature = (res.data.usageNature as string).split(',') as any;
  if (Array.isArray(form.value.insuranceInfo)) {
    form.value.insuranceInfo.forEach((item: any) => {
      if (item.insuranceBeginDate && item.insuranceEndDate) {
        item.insuranceDate = [item.insuranceBeginDate, item.insuranceEndDate];
      } else {
        item.insuranceDate = [];
      }
    });
  }
  if (Array.isArray(form.value.annualInspectionRecord)) {
    form.value.annualInspectionRecord.forEach((item: any) => {
      if (item.validityBeginDate && item.validityEndDate) {
        item.validityDate = [item.validityBeginDate, item.validityEndDate];
      } else {
        item.validityDate = [];
      }
    });
  }

  // 处理后端返回的字符串数值
  const data = { ...res.data } as VehicleBasicInfoForm;

  // 处理保险信息中的保费字段
  if (data.insuranceInfo && data.insuranceInfo.length) {
    data.insuranceInfo.forEach((item) => {
      if (item.premium !== undefined && item.premium !== null) {
        item.premium = Number(item.premium);
      }
    });
  }

  // 处理维修记录中的费用字段
  if (data.repairRecord && data.repairRecord.length) {
    data.repairRecord.forEach((item) => {
      if (item.cost !== undefined && item.cost !== null) {
        item.cost = Number(item.cost);
      }
    });
  }

  Object.assign(form.value, data);
  dialog.visible = true;
  dialog.title = '修改车辆基本信息';
};

/** 提交按钮 */
const submitForm = () => {
  vehicleBasicInfoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      const formData = { ...form.value };
      formData.usageNature = (form.value.usageNature as unknown as string[]).join(',') as any;
      // 确保所有数值字段都是数字类型
      if (formData.insuranceInfo && formData.insuranceInfo.length) {
        formData.insuranceInfo.forEach((item) => {
          if (item.premium !== undefined) {
            item.premium = Number(item.premium);
          }
        });
      }

      if (formData.repairRecord && formData.repairRecord.length) {
        formData.repairRecord.forEach((item) => {
          if (item.cost !== undefined) {
            item.cost = Number(item.cost);
          }
        });
      }

      if (form.value.id) {
        await updateVehicleBasicInfo(formData).finally(() => (buttonLoading.value = false));
      } else {
        await addVehicleBasicInfo(formData).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: VehicleBasicInfoVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除车辆？').finally(() => (loading.value = false));
  await delVehicleBasicInfo(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'vehicle/vehicleBasicInfo/export',
    {
      ...queryParams.value
    },
    `vehicleBasicInfo_${new Date().getTime()}.xlsx`
  );
};

const addDomain = () => {
  if (form.value.insuranceInfo.length > 0) {
    const lastItem = form.value.insuranceInfo[form.value.insuranceInfo.length - 1];
    if (!lastItem.insuredDate || !lastItem.insuranceCompany || !lastItem.insuranceType || !lastItem.insuranceDate || !lastItem.premium) {
      proxy?.$modal.msgError('请完善上一条车辆保险信息');
      return;
    }
  }

  form.value.insuranceInfo.push({
    insuredDate: undefined,
    insuranceCompany: undefined,
    insuranceType: undefined,
    insuranceBeginDate: undefined,
    insuranceEndDate: undefined,
    premium: undefined
  });
};
const addDomain1 = () => {
  if (form.value.repairRecord.length > 0) {
    const lastItem = form.value.repairRecord[form.value.repairRecord.length - 1];
    if (!lastItem.repairDate || !lastItem.manufacturer || !lastItem.repairItem || !lastItem.cost) {
      proxy?.$modal.msgError('请完善上一条车辆维修记录');
      return;
    }
  }

  form.value.repairRecord.push({
    repairDate: undefined,
    manufacturer: undefined,
    repairItem: undefined,
    cost: undefined
  });
};

const addDomain2 = () => {
  if (form.value.annualInspectionRecord.length > 0) {
    const lastItem = form.value.annualInspectionRecord[form.value.annualInspectionRecord.length - 1];
    if (!lastItem.inspectionDate || !lastItem.inspectionResult || !lastItem.validityDate) {
      proxy?.$modal.msgError('请完善上一条车辆年检记录');
      return;
    }
  }

  form.value.annualInspectionRecord.push({
    inspectionDate: undefined,
    inspectionResult: undefined,
    validityDate: undefined,
    validityBeginDate: undefined,
    validityEndDate: undefined
  });
};
const removeDomain = (domain: any, field: string) => {
  const index = form.value[field].indexOf(domain);
  if (index > -1) {
    form.value[field].splice(index, 1);
  }
};
const changeInsuranceDate = (domain: any, index: number) => {
  form.value.insuranceInfo[index].insuranceBeginDate = domain[0];
  form.value.insuranceInfo[index].insuranceEndDate = domain[1];
};

const changeValidityDate = (event: any, index: number) => {
  form.value.annualInspectionRecord[index].validityBeginDate = event[0];
  form.value.annualInspectionRecord[index].validityEndDate = event[1];
};

const handleInfo = (row: any) => {
  drawerRef.value?.isOpen(row.id);
};
onMounted(() => {
  getList();
});
</script>
<style lang="css" scoped>
.tableData {
  border: 0;
  font-size: 26rpx;
  border-collapse: collapse;
  border-spacing: 0;
  width: 100%;
  border-radius: 8rpx;
}

.tableData td {
  height: 40px;
  padding: 5px;
  color: #666666;
  border: 1rpx solid #ededed;
  border-collapse: collapse !important;
  text-align: center;
}

.tableData th {
  height: 40px;
  font-family: PingFang SC-Bold, PingFang SC;
  font-weight: bold;
  color: #333333;
  background: #f8f8f9;
  border: 1px solid #ededed;
}
</style>
